package Form;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import CACA.CACAClassifier;
import CACA.Rule;
import DataSetPackage.Attribute;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.ScrollPaneConstants;
import java.awt.SystemColor;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ClassifyForm extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTable table;
	
	private String [] columnNames;
	private CACAClassifier _cacaClassifier;
	private JTextField textField;
	JLabel lblInfo = new JLabel("info");
	JLabel lblSupp = new JLabel("supp");
	JLabel lblConf = new JLabel("conf");
	/**
	 * Create the dialog.
	 */
	public ClassifyForm(CACAClassifier classifier) {
		setLocationByPlatform(true);
		_cacaClassifier = classifier;
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 801, 227);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);

		ArrayList<Attribute> att = _cacaClassifier.getAttributes();
		
		columnNames = new String[att.size()];
		int ind = 0;
		for (Attribute attribute : att) {
			columnNames[ind++] = attribute.getName();
		}
		contentPanel.setLayout(new GridLayout(0, 1, 0, 0));

		{
			table = new JTable();
			table.setShowHorizontalLines(false);
			table.setRowSelectionAllowed(false);
			table.setFillsViewportHeight(true);
			table.setBackground(SystemColor.inactiveCaptionBorder);
			table.setModel(new DefaultTableModel(
					new Object[][] { { null, null, null, null }, }, 
					columnNames
					
					));			
			JScrollPane scrollPane = new JScrollPane(table);
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
			table.setPreferredSize(new Dimension(800, 40));
			scrollPane.setPreferredSize(new Dimension(800, 40));
			contentPanel.add(scrollPane);
		}
		
		
		for (int i = 0; i < columnNames.length; i++) {
			TableColumn tableColumn = table.getColumnModel().getColumn(i);

			JComboBox<String> comboBox = new JComboBox<String>();
			ArrayList<String> nominals = _cacaClassifier.getAttributes().get(i).getNominals();
			comboBox.addItem("?");
			for (Iterator<String> iterator = nominals.iterator(); iterator
						.hasNext();) {
				String nominal = (String) iterator.next();
				comboBox.addItem(nominal);
			}
			if (i == _cacaClassifier.getClassIndex()) {
				comboBox.setEnabled(false);
				comboBox.setBackground(Color.PINK);
			}
			comboBox.setSelectedIndex(0);
			tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
			
		}
				
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Predict");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					}
				});
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						
						int data[] = new int[columnNames.length];
	
						for(int i =0;i<columnNames.length;i++){
							DefaultCellEditor cell = (DefaultCellEditor) table
										.getCellEditor(1, i);
							@SuppressWarnings("unchecked")
							JComboBox<String> combobox = (JComboBox<String>)cell.getComponent();
							data[i] = combobox.getSelectedIndex()-1; 
						}
						DecimalFormat pf = new DecimalFormat("###.##%");
						Rule rule = _cacaClassifier.getOrTree().getPredictedRule(data);
						textField.setText(_cacaClassifier.getClassAttribute().getNominal(rule.getLabel()));
						lblInfo.setText(_cacaClassifier.rule2String(rule));
						lblConf.setText(pf.format(rule.getConf()));
						lblSupp.setText(pf.format((double)rule.getSuppCount()/_cacaClassifier.getNoInstances()));

					}
				});
				okButton.setActionCommand("Predict");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						ClassifyForm.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(new GridLayout(0, 1, 0, 0));
			{
				JPanel panel_1 = new JPanel();
				FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
				flowLayout.setAlignment(FlowLayout.LEFT);
				panel.add(panel_1);
				{
					JLabel lblPredictedClass = new JLabel("Predicted Class :");
					panel_1.add(lblPredictedClass);
				}
				{
					textField = new JTextField();
					textField.setEditable(false);
					panel_1.add(textField);
					textField.setColumns(10);
				}
			}
			{
				JPanel panel_1 = new JPanel();
				FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
				flowLayout.setAlignment(FlowLayout.LEFT);
				panel.add(panel_1);
				{
					JLabel lblRuleInformation = new JLabel("Rule :");
					panel_1.add(lblRuleInformation);
				}
				
					
				panel_1.add(lblInfo);
				
			}
			{
				JPanel panel_1 = new JPanel();
				FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
				flowLayout.setAlignment(FlowLayout.LEFT);
				panel.add(panel_1);
				{
					JLabel lblNewLabel = new JLabel("Supp :");
					panel_1.add(lblNewLabel);
				}
				

				panel_1.add(lblSupp);
				
			}
			{
				JPanel panel_1 = new JPanel();
				FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
				flowLayout.setAlignment(FlowLayout.LEFT);
				panel.add(panel_1);
				{
					JLabel lblNewLabel_1 = new JLabel("Conf :");
					panel_1.add(lblNewLabel_1);
				}
				
				panel_1.add(lblConf);
				
			}
		}
	}
	

}
